package org.huangxiaozhe.adventure;
import org.huangxiaozhe.adventure.common.ConstantUtil;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class PlayActivity extends Activity implements OnTouchListener, OnGestureListener{
	
	GestureDetector mGestureDetector;  
	private int verticalMinDistance = 35;  
	private int minVelocityX         = 8;  
	private int minVelocityY         = 8; 
	
	GameView gameView;//GameView������
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return super.onTouchEvent(event);
	}

	@Override
	protected void onPause() {
		Log.d(ConstantUtil.tag, "onPause");
		gameView.Stop();
		super.onPause();
	}

	@Override
	protected void onResume() {
		Log.d(ConstantUtil.tag, "onResume");
		super.onResume();
		//gameView.ThreadNotify();
	}

    @Override
	protected void onRestart() {
    	Log.d(ConstantUtil.tag, "onRestart");
    	gameView.prepareThread();
    	gameView.Stop();
		super.onRestart();
	}

	@Override
	protected void onStart() {
		Log.d(ConstantUtil.tag, "onStart");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.d(ConstantUtil.tag, "onStop");
    	super.onStop();
	}

	@Override
	protected void onDestroy() {
		Log.d(ConstantUtil.tag, "onDestroy");
    	super.onDestroy();
	}

	public void onCreate(Bundle savedInstanceState) {//�����Ǳ�����
        super.onCreate(savedInstanceState);
        Log.d(ConstantUtil.tag, "onCreate");
    	
        Intent intent = this.getIntent();
        intent.getStringExtra("abc");
        //ȫ��
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,  
		              WindowManager.LayoutParams.FLAG_FULLSCREEN);
		gameView = new GameView(PlayActivity.this);
		//ȥ��Ϸ����
		this.setContentView(gameView);
		
		//�������Ƶ���Ӧ
		mGestureDetector = new GestureDetector((OnGestureListener) this);
		gameView.setOnTouchListener(this);    
	    gameView.setLongClickable(true);    
    }

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
	}

	//��ָ��ѹ��Ļ
	public boolean onDown(MotionEvent event) {
		
		return false;
	}

	//����������ҷ�ҳ����
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		
		float tan = Math.abs(e1.getX() - e2.getX()) / Math.abs(e1.getY() - e2.getY());
		if(tan >= 1.5)
		{
			if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocityX) {  
				gameView.Stop();
		    } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocityX) {  
		       gameView.Start();
		    }
		}
		if(gameView.pig.currentTool == 4 && tan <= 0.666666){
			if(Math.abs(velocityY) > minVelocityY){
				gameView.pig.planeGo(e2.getY() - e1.getY());
			}
		}
		return false;
	}

	//��ָ������Ļ
	public void onLongPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	//��ָ�����Ļ
	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public boolean onSingleTapUp(MotionEvent event) {
		int tool = gameView.tools.isInclude(event.getX(), event.getY());
		if(tool >= 0){
			gameView.pig.useTool(tool);
			Toast.makeText(this, "Use Tool:" + (tool+1), Toast.LENGTH_SHORT).show();
		}
		else{
			//ʹ��̹�˹���ʱ�������
			if(gameView.pig.getCurTool() == 3){
				gameView.pig.shoot(event.getX(), event.getY());
			}
		}
		return false;
	}

	public boolean onTouch(View arg0, MotionEvent event) {
		if(gameView.pig.currentTool == 1){
			int action = event.getAction();
			if(action == MotionEvent.ACTION_DOWN){
				gameView.downFinger(event.getX(), event.getY());
			}
			else if(action == MotionEvent.ACTION_MOVE){
				gameView.moveFinger(event.getX(), event.getY());
			}
			else if(action == MotionEvent.ACTION_UP){
				gameView.upFinger(event.getX(), event.getY());
			}
		}
		return mGestureDetector.onTouchEvent(event);
	}
}